using System;
using System.Collections.Generic;
using System.Text;
using BusinessObjects;
using System.Collections;
using Helper;
using GUI.Properties;
using System.Data;

namespace GUI
{
    public class ChameleonManager
    {
        public Profile GetProfile(string parProfileName)
        {
            Profile insProfile = new Profile();
            Hashtable ht = (Hashtable)BinarySerializer.BinaryTo(Settings.Default.Profiles);
            return (Profile)ht[parProfileName];
        }
        public List<Profile> GetProfiles()
        {
            List<Profile> lstProfile = new List<Profile>();
            if (Settings.Default.Profiles != "")
            {
                Hashtable ht = (Hashtable)BinarySerializer.BinaryTo(Settings.Default.Profiles);
                foreach (DictionaryEntry de in ht)
                {
                    Profile insProfile = (Profile)de.Value;
                    lstProfile.Add(insProfile);
                }
            }
            ObjectPropertyComparer<Profile> opc = new ObjectPropertyComparer<Profile>("ProfileName");
            lstProfile.Sort(opc);
            return lstProfile;
        }
        public string SaveProfile(Profile parProfile, bool openedForEdit)
        {
            if (Settings.Default.Profiles == "")
            {
                Hashtable ht = new Hashtable();
                Settings.Default.Profiles = BinarySerializer.ToBinary(ht);
                Settings.Default.Save();
            }
            Hashtable ht1 = (Hashtable)BinarySerializer.BinaryTo(Settings.Default.Profiles);
            if (!openedForEdit)
            {
                foreach (DictionaryEntry de in ht1)
                {
                    if (((Profile)de.Value).ProfileName == parProfile.ProfileName)
                    {
                        return "There is already a profile with the same name.";
                    }
                }
            }

            ht1[parProfile.ProfileName] = parProfile;
            Settings.Default.Profiles = BinarySerializer.ToBinary(ht1);
            Settings.Default.Save();
            return "";
        }
        public void DeleteProfile(string parProfileName)
        {
            Hashtable ht = (Hashtable)BinarySerializer.BinaryTo(Settings.Default.Profiles);
            ht.Remove(parProfileName);
            Settings.Default.Profiles = BinarySerializer.ToBinary(ht);
            Settings.Default.Save();
        }
        public DataTable GetProfilesDataTable()
        {
            DataTable dtProfiles = new DataTable();
            dtProfiles.Columns.Add("ProfileName", typeof(string));
            dtProfiles.Columns.Add("IpAddress", typeof(string));
            dtProfiles.Columns.Add("SubnetMask", typeof(string));
            dtProfiles.Columns.Add("DefaultGateway", typeof(string));
            dtProfiles.Columns.Add("PreferredDns", typeof(string));
            dtProfiles.Columns.Add("AlternateDns", typeof(string));
            if (Settings.Default.Profiles != "")
            {
                Hashtable ht = (Hashtable)BinarySerializer.BinaryTo(Settings.Default.Profiles);
              
                foreach (DictionaryEntry de in ht)
                {
                    DataRow dr = dtProfiles.NewRow();
                    dr["ProfileName"] = ((Profile)de.Value).ProfileName;
                    dr["IpAddress"] = ((Profile)de.Value).IpAddress;
                    dr["SubnetMask"] = ((Profile)de.Value).SubnetMask;
                    dr["DefaultGateway"] = ((Profile)de.Value).DefaultGateway;
                    dr["PreferredDns"] = ((Profile)de.Value).PreferredDns;
                    dr["AlternateDns"] = ((Profile)de.Value).AlternateDns;
                    dtProfiles.Rows.Add(dr);
                }
            }
            dtProfiles.Select("", "ProfileName");
            return dtProfiles;
        }
    }
}
